package org.example.crm.clue.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.sun.deploy.security.BlockedException;
import org.example.crm.clue.Result;
import org.example.crm.clue.entity.Clue;
import org.example.crm.clue.service.IClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 线索 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-06-28
 */
@RestController
@RequestMapping("/clue")
@RefreshScope
public class ClueController {
    @Autowired
    private IClueService clueService;

    @Value("${clue.id.min}")
    private int min;

    @Value("${clue.id.max}")
    private int max;

    @Value("${cache.time}")
    private int time;

    @SentinelResource(value = "clues.get", blockHandler = "blockGet")
    @GetMapping("/{id}")
    public Result<Clue> get(@PathVariable Long id){
        System.out.println("min = " + min+"max="+max+"time="+time);
        if (id > max || id < min) {
            return Result.error("id超出了范围");
        }
        return Result.ok(clueService.getById(id));
    }

    public Result<Clue> blockGet(Long id, BlockException ex){
        return Result.error("/clues/get/{id}被限流了，请稍后访问");
    }
}

